//
//  ViewController.m
//  frame_bounds_center_transform
//
//  Created by 李煜 on 2024/3/8.
//https://www.jianshu.com/p/6272531661be

#import "ViewController.h"

@interface ViewController ()
@property (nonatomic,strong) UIView *aView;
@property (nonatomic,strong) UIView *bView;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    UIView *a = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 40, 40)];
    a.backgroundColor = [UIColor orangeColor];
    [self.view addSubview:a];
    self.aView = a;

    UIView *b = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 40, 40)];
    b.backgroundColor = [UIColor blueColor];
    [self.view addSubview:b];
    self.bView = b;
    b.transform = CGAffineTransformMakeTranslation(100, 0);
    b.transform = CGAffineTransformMakeScale(1.0, 2.0);

    CGAffineTransform tra = [self transformFromRect:self.aView.frame toRect:self.bView.frame];

    NSLog(@"--%@",NSStringFromCGAffineTransform(tra));
}

-(CGAffineTransform)transformFromRect:(CGRect)fromA toRect:(CGRect)fromeB{
    // 移动
    CGAffineTransform moveTrans = CGAffineTransformMakeTranslation(CGRectGetMidX(fromeB) - CGRectGetMidX(fromA), CGRectGetMidY(fromeB) - CGRectGetMidY(fromA));
    // 缩放
    CGAffineTransform scaleTrans = CGAffineTransformMakeScale(fromeB.size.width / fromA.size.width, fromeB.size.height / fromA.size.height);
    // 用来连接两个变换效果并返回。返回的t = t1 * t2
    // CGAffineTransformConcat(CGAffineTransform t1, CGAffineTransform t2)
    return CGAffineTransformConcat(scaleTrans, moveTrans);
}

@end
